package com.sctvcloud.dongqu.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.jiongbull.jlog.JLog;
import com.ruihang.generalibrary.ui.util.IListData;
import com.ruihang.generalibrary.utils.DensityUtil;
import com.ruihang.generalibrary.utils.ListUtils;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.base.BaseActivity;
import com.sctvcloud.dongqu.beans.AddrInfo;
import com.sctvcloud.dongqu.beans.FUsers;
import com.sctvcloud.dongqu.beans.SingleResult;
import com.sctvcloud.dongqu.http.AbsListNetCallback;
import com.sctvcloud.dongqu.http.AbsNetCallBack;
import com.sctvcloud.dongqu.http.NetUtils;
import com.sctvcloud.dongqu.ui.adapter.AddressSelectAdapter;
import com.sctvcloud.dongqu.ui.util.IListShowData;
import com.sctvcloud.dongqu.ui.util.RBaseItemDecoration;
import com.sctvcloud.dongqu.ui.utils.TitleUtils;
import com.sctvcloud.dongqu.utils.UserManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

public class AddressSelectActivity extends BaseActivity {

    @BindView(R.id.address_select_recycler)
    protected RecyclerView recyclerView;
    private ArrayList<AddrInfo> addrInfos;
    private AddressSelectAdapter adapter;
//    private ArrayList<IListShowData> addData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle(R.string.select_address);
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
//        DividerItemDecoration did = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
//        did.;
        recyclerView.addItemDecoration(new RBaseItemDecoration(DensityUtil.dip2px(this, 15)
                , ContextCompat.getColor(this, R.color.colorGrayMain)));
//        addData = new ArrayList<>();
//        addButton();
        addrInfos = (ArrayList<AddrInfo>) getIntent().getSerializableExtra(EX_DATA);
        if (addrInfos == null) {
            addrInfos = new ArrayList<>();
        }
        initDatas(addrInfos);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        getAddressListData();
    }

    private void getAddressListData() {
        FUsers fu = UserManager.getInstance().getUser();
        if (fu == null) {
            toLogin();
            return;
        }
        showProgress("数据读取中...");
        NetUtils.getNetAdapter().getAddrList(getOwnerName(), fu.getPhoneNumber(), fu.getToken(), new AbsListNetCallback<AddrInfo>(AddrInfo.class) {
            @Override
            public void onSuc(List<AddrInfo> addrInfos) {
                initDatas(addrInfos);
                closeProgress();
            }
        });
    }

    @Override
    protected void initInject() {
        initButterknifeBiner(R.layout.activity_address_select);
    }

    private synchronized void initDatas(List<AddrInfo> list) {
        AddrInfo addrInfo = new AddrInfo();
        addrInfo.setType(3);
        list.add(addrInfo);
        if (adapter == null) {
            adapter = new AddressSelectAdapter(this, list);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.setData(list);
        }
        adapter.setAddAddressClick(new AddressSelectAdapter.AddAddressClick() {
            @Override
            public void onAddClick() {
                startActivityForResult(new Intent(AddressSelectActivity.this, AddressAddActivity.class), 989);
            }

            @Override
            public void onSelectClick(AddrInfo addr) {
                if (addr.getType() != 3) {
                    Intent intent = new Intent();
                    intent.putExtra(EX_DATA, addr);
                    setResult(999, intent);
                    setDefaultAddress(addr);
                }
            }
        });
    }

    /**
     * 设置默认地址
     * @param addr
     */
    private void setDefaultAddress(AddrInfo addr) {
        if (UserManager.isLoginS()) {
            String userPhone = UserManager.getInstance().getUser().getPhoneNumber();
            String token = UserManager.getInstance().getToken();
            ArrayMap<String, Object> p = new ArrayMap<>();
            p.put("userId", userPhone);
            p.put("token", token);
            p.put("addressCode", addr.getAddrId());
            NetUtils.getNetAdapter().setDefaultAddr(getOwnerName(), p, new AbsNetCallBack<SingleResult>(SingleResult.class) {
                @Override
                public void onSuc(SingleResult info) {
                    if(info.isSuccess()) {
                        JLog.e("AddressSelectActivity", "设置默认地址成功");
                    }
                }

                @Override
                public void onEnd() {
                    super.onEnd();
                    finish();
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 989 && data != null) {
            AddrInfo addr = (AddrInfo) data.getSerializableExtra(EX_DATA);
            Intent intent = new Intent();
            intent.putExtra(EX_DATA, addr);
            setResult(999, intent);
            setDefaultAddress(addr);
        }
    }

    //    private void addButton() {
//        addData = new ArrayList<>();
//        TestAddressData addressData = new TestAddressData();
//        addressData.setType(3);
//        addData.add(addressData);
//    }


}
